﻿using BusinessLogicLayer;
using Equin.ApplicationFramework;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PresentationLayer
{
    public partial class FormThemNVDA : Form
    {
        public FormThemNVDA()
        {
            InitializeComponent();
        }

        private void FormThemNVDA_Load(object sender, EventArgs e)
        {
            ActionResultAsList<NhanVien> ar = new DuAnManager().LayDSNVThemDA();
            if (ar.Success)
            {
                dgvDSNV.AutoGenerateColumns = false;
                dgvDSNV.DataSource = new BindingListView<NhanVien>(ar.ObjectList);
                foreach (DataGridViewRow row in dgvDSNV.Rows)
                {
                    string maNV = row.Cells["MaNhanVien"].Value.ToString();
                    if (DSNV.Exists(k => k.MaNhanVien == maNV))
                        row.Cells["Chon"].Value = true;
                }
                listTemp = LayDSNV();
            }
            else
            {
                MessageBox.Show(ar.Exceptions[ar.Exceptions.Count - 1].Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
            
        }
        public List<NhanVien> DSNV = new List<NhanVien>();
        List<string> listTemp = new List<string>();
        private void btnxOK_Click(object sender, EventArgs e)
        {
            List<NhanVien> l = (dgvDSNV.DataSource as BindingListView<NhanVien>).DataSource as List<NhanVien>;
            List<string> dsManv = LayDSNV();
            DSNV.Clear();
            foreach (var item in dsManv)
            {
                using (var db = new QLNVDbContext())
                {
                    NhanVien nv = db.NhanViens.Single(k => k.MaNhanVien == item);
                    DSNV.Add(nv);
                }
            }
            Close();
        }

        private List<string> LayDSNV()
        {
            List<string> list = new List<string>();
            foreach (DataGridViewRow row in dgvDSNV.Rows)
            {
                if (Convert.ToBoolean(row.Cells["Chon"].Value))
                    list.Add(row.Cells["MaNhanVien"].Value.ToString());
            }
            return list;
        }

        private void dgvDSNV_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            foreach (DataGridViewRow row in dgvDSNV.Rows)
            {
                string maNV = row.Cells["MaNhanVien"].Value.ToString();
                if (listTemp.Contains(maNV))
                    row.Cells["Chon"].Value = true;
            }
        }

        private void dgvDSNV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            btnxOK.Focus();
            listTemp = LayDSNV();
        }
    }
}
